package org.goldfish.library_system.controller;

import org.goldfish.library_system.entity.BorrowRecord;
import org.goldfish.library_system.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/borrow-records")
public class BorrowRecordController {
    @Autowired
    private BorrowRecordService borrowRecordService;

    // 获取所有借阅记录
    @GetMapping
    public List<BorrowRecord> getAllBorrowRecords() {
        return borrowRecordService.findAllBorrowRecords();
    }

    // 根据读者 ID 获取借阅记录
    @GetMapping("/reader/{readerId}")
    public List<BorrowRecord> getBorrowRecordsByReaderId(@PathVariable Long readerId) {
        return borrowRecordService.findBorrowRecordsByReaderId(readerId);
    }

    // 根据图书 ID 获取借阅记录
    @GetMapping("/book/{bookId}")
    public List<BorrowRecord> getBorrowRecordsByBookId(@PathVariable Long bookId) {
        return borrowRecordService.findBorrowRecordsByBookId(bookId);
    }

    // 获取所有逾期记录
    @GetMapping("/overdue")
    public List<BorrowRecord> getOverdueRecords() {
        return borrowRecordService.findOverdueRecords();
    }

    // 添加或更新借阅记录
    @PostMapping
    public BorrowRecord addOrUpdateBorrowRecord(@RequestBody BorrowRecord borrowRecord) {
        return borrowRecordService.saveBorrowRecord(borrowRecord);
    }

    // 删除借阅记录
    @DeleteMapping("/{id}")
    public void deleteBorrowRecord(@PathVariable Long id) {
        borrowRecordService.deleteBorrowRecord(id);
    }
}